package com.kadron.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.kadron.base.KBase;
import com.kadron.common.KUtil;
import com.kadron.enumeration.CommentType;

@Entity
@Table(name = "k_comment")
public class Comment extends KBase implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -138894430940275537L;

	private CommentType type = CommentType.WALL;

	private User user;
	private Item item;

	private String name;
	private boolean approved;
	private String comment;
	
	private String ipAddress;
	private long approvedBy;
	
	private String userName = "";//Transient property

	private Comment() {
		super();
	}

	public Comment(CommentType type, String name, String comment) {
		this();
		this.type = type;
		this.name = name;
		this.comment = comment;
	}

	public Comment(CommentType type, User user, Item item, String name,
			String comment) {
		this();
		this.type = type;
		this.user = user;
		this.item = item;
		this.name = name;
		this.comment = comment;
	}

	@ManyToOne
	@JoinColumn(name = "user_id", updatable = false)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Column(name = "approved", nullable = false)
	public boolean isApproved() {
		return approved;
	}

	public void setApproved(boolean approved) {
		this.approved = approved;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "comment", nullable = false, columnDefinition = "TEXT")
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@ManyToOne
	@JoinColumn(name = "item_id", updatable = false)
	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	@Column(name = "type", nullable = false, updatable = false)
	public CommentType getType() {
		return type;
	}

	public void setType(CommentType type) {
		this.type = type;
	}

	@Column(name = "ip_address", nullable = false, updatable = false)
	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	@Column(name = "approved_by")
	public long getApprovedBy() {
		return approvedBy;
	}
	
	public void setApprovedBy(long approvedBy) {
		this.approvedBy = approvedBy;
	}

	@Transient
	public String getUserName() {
		if (KUtil.isEmpty(userName)) {
			if (user != null) {
				userName = user.getFullName();
			} else {
				userName = name;				
			}
		}
		return userName;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Comment other = (Comment) obj;
		if (id != other.id)
			return false;
		return true;
	}

}
